package com.cee.admin.modules.prod.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.admin.common.BaseController;
import com.cee.common.exception.GlobalException;
import com.cee.common.spring.SpringContextHolder;
import com.cee.common.web.result.Result;
import com.cee.common.web.result.ResultCode;
import com.cee.domain.modules.pro.entity.ProAfforestCarbon;
import com.cee.domain.modules.prod.entity.ProdProduct;
import com.cee.domain.modules.prod.entity.ProdProductDataReport;
import com.cee.domain.modules.prod.vo.ProdProductVo;
import com.cee.domain.modules.sys.entity.SysFrontUser;
import com.cee.ds.common.enums.ProductIndustry;
import com.cee.ds.common.utils.BasePrincipalUtils;
import com.cee.ds.modules.prod.service.IProdProductDataReportService;
import com.cee.ds.modules.prod.service.IProdProductService;
import com.cee.ds.modules.sys.service.ISysFrontUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品表 前端控制器
 * </p>
 *
 * @author hys
 * @since 2024-03-04
 */
@Tag(name = "产品管理-产品")
@RestController
@RequestMapping("/api/prod/product")
public class ProdProductController extends BaseController {

    @Autowired
    private IProdProductService prodProductService;

    @Autowired
    private IProdProductDataReportService iProdProductDataReportService;

    @Operation(summary = "分页查询产品")
    @GetMapping("/page")
    public Result<Page<ProdProductVo>> page(@RequestParam Long current, @RequestParam Long size, ProdProductVo product){
        handleOrder(product);
        product.setCityId(BasePrincipalUtils.currentUser().getCityId());
        return success(prodProductService.queryPageVo(new Page<>(current,size),product));
    }

//    @Operation(summary = "新增产品")
//    @PostMapping("/save")
//    public Result<Boolean> save(@RequestBody ProdProduct product){
//        product.setExamineStatus("1");
//        return success(prodProductService.save(product));
//    }

//    @Operation(summary = "修改产品")
//    @PostMapping("/update")
//    public Result<Boolean> update(@RequestBody ProdProduct product){
//        return success(prodProductService.updateById(product));
//    }

    @Operation(summary = "查看产品详情")
    @GetMapping("/get/{id}")
    public Result<ProdProduct> get(@PathVariable("id") Long id) {
        ProdProduct prodProduct = prodProductService.getByIdOrEx(id);
        Integer cityId = BasePrincipalUtils.currentUser().getCityId();
        if (cityId != null) {
            SysFrontUser frontUser = SpringContextHolder.getBean(ISysFrontUserService.class).getById(prodProduct.getBelongUser());
            if (frontUser != null && !Objects.equals(cityId, frontUser.getCityId())) {
                throw new GlobalException(ResultCode.NOT_PERMISSION);
            }
        }
        return success(prodProduct);
    }

    @Operation(summary = "删除产品")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("prod:product:delete")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        if (CollectionUtils.isNotEmpty(iProdProductDataReportService.lambdaQuery().eq(ProdProductDataReport::getProductId, id).list())) {
            throw new GlobalException("该产品下存在填报，不可删除");
        }
        Integer cityId = BasePrincipalUtils.currentUser().getCityId();
        if (cityId != null) {
            ProdProduct prodProduct = prodProductService.getByIdOrEx(id);
            SysFrontUser frontUser = SpringContextHolder.getBean(ISysFrontUserService.class).getById(prodProduct.getBelongUser());
            if (frontUser != null && !Objects.equals(cityId, frontUser.getCityId())) {
                throw new GlobalException(ResultCode.NOT_PERMISSION);
            }
        }
        return success(prodProductService.removeById(id));
    }

//    @PostMapping("/examine")
//    @Operation(summary = "审核产品申请")
//    public Result<Boolean> examine(@RequestParam Long id, @RequestParam String examineStatus, String examineNote){
//        ProdProduct product = new ProdProduct();
//        product.setId(id);
//        product.setExamineStatus(examineStatus);
//        product.setExamineNote(examineNote);
//        product.setExamineTime(new Date());
//        product.setExamineUserId(BasePrincipalUtils.currentUserId());
//        return success(prodProductService.updateById(product));
//    }

    @Operation(summary = "查询供应厂商列表")
    @GetMapping("/supplier/list")
    public Result<List<ProdProductVo>> list(@RequestParam String industry, @RequestParam String productType) {
        return success(prodProductService.listSupplier(industry, productType, null));
    }

    @GetMapping("/generalBox")
    @Operation(summary = "获取通用行业产品的产品类别")
    public Result<Set<String>> box() {
        return success(prodProductService.lambdaQuery()
                .select(ProdProduct::getProductType)
                .eq(ProdProduct::getIndustry, ProductIndustry.GENERAL.code)
                .list()
                .stream()
                .map(ProdProduct::getProductType).collect(Collectors.toSet()));
    }
}
